#include "rc522.h"
#include "MQTT_Client.h"

void setup()
{
  Serial.begin(115200);
  while (!Serial)
  {
    ;
  }
  
  SPI.begin();
  rfid.PCD_Init();
  EEPROM.begin(EEPROM_SIZE);

  /* EEPROM 版本号初始化 */
  if (EEPROM.read(VERSION_ADDR) != CURRENT_VERSION)
  {
    deleteAllCards();
    EEPROM.write(VERSION_ADDR, CURRENT_VERSION);
    EEPROM.commit();
  }

  Serial.println("ESP32 MFRC522 读卡器示例（MQTT版）");
  Serial.println("支持串口和MQTT控制");
  Serial.println("串口命令：write | read | delete | delete 0x12 0x34 0x56 0x78");
  Serial.println("MQTT主题：Android_Get (接收命令) | Android_Set (发送状态)");
  
  // 初始化WiFi和MQTT
  setupWiFi();
  setupMQTT();
}

void loop()
{
  // 处理串口输入（保留原有功能）
  handleSerialInput();
  
  // 处理RFID读卡
  readAndProcessCard();
  
  // 处理MQTT消息
  mqttLoop();
}